<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      .box {
        position: relative;
        top: 20px;
        left: 20px;
        width: 200px;
        height: 200px;
        background-color: blanchedalmond;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <script>
      window.onload = function () {
        const box = document.querySelector(".box");
        box.onmousedown = function (e) {
          console.log(e.pageX);
          console.log(e.pageY);
          let x = e.pageX - parseInt(getComputedStyle(box).left);
          let y = e.pageY - parseInt(getComputedStyle(box).top);
          console.log(x, y);

          window.onmousemove = function (e) {
            box.style.left = e.pageX - x + "px";
            box.style.top = e.pageY - y + "px";
          };

          window.onmouseup = function () {
            window.onmousemove = null;
          };
        };
      };
    </script>
  </body>
</html>
